var libs = require('libs/libs');
var router = require('libs/router').router;
var route = require('libs/router').route;
var url = libs.urlparse(location.href);
var wxShare = require('modules/weixin/share')
SA.set('USER', {login: false})
var api = libs.api;
var comm = require('./comm')
var _login;
var _hash;
var _reurl;

// libs.portrait('100px', function(){
//     libs.msgtips('建议竖屏', 'center')
// })

if (url.params.login){
    SA.set('USER', {login: url.params.login})
    _login = url.params.login;
    // comm.replaceState('login')
}

if (url.params.reurl){
    _reurl = url.params.reurl
    SA.append('USER', {reurl: _reurl})
    comm.replaceState('reurl')
}

if (url.params.hash){
    _hash = url.params.hash
    comm.replaceState('hash')
}

// 微信分享
var _share_data = {
    title: '大白管家',
    desc: '大白管家，最贴心的管家',
    link: 'http://www.dabai360.com/',
    imgUrl: 'http://functest.dabai360.com/images/ic_default_head.png'
}
SA.set('_wxShare', _share_data, function( data ){
    var title = document.title;
    var _hashName;
    var $link = '';
    var $imgUrl = '';
    var url = libs.urlparse(window.location.href);
    if (url.hash || _hash || _reurl){
        _hashName = _hash || _reurl || url.hash
        // if (_hashName === 'express'){
        //     title = '大白管家快递收件服务'
        // }
        // if (_hashName === 'send/send'){
        //     title = '大白管家快递送件服务'
        // }
        // if (_hashName === 'water/send'){
        //     title = '大白管家送水服务'
        // }
        // if (_hashName === 'water/card'){
        //     title = '购买水卡'
        // }
        if (_hashName === 'uc/siteIntroduce'){
            var $user = SA.get('USER'),
                $c_id = $user.info.currentCommunity.communityId

                title = $user.info.currentCommunity.cityName + $user.info.currentCommunity.communityName + '网点介绍'
                $link = 'http://' + $user.info.domainName + '/imapi/h5/index.html?communityId='+$c_id+'#!/personCenter'

                if (data.provideImg){
                    _img = data.provideImg
                    $imgUrl =  _img.length ? _img[0].extUrl : ''
                }

                wxShare({
                    title: '网点介绍',
                    desc: title,
                    link: $link,
                    imgUrl: $imgUrl
                })
        }
    }
    // console.log(url);
    // console.log(title);
})

wxShare(_share_data)



//添加自己的路由
route.init({
    'show':require('./_component/_show'),    //演示模块，可删除

    'send/details':require('./_component/send/sendDetails'), //快递寄件-订单详情
    'send/send':require('./_component/send/send'), //快递寄件-下单页
    'send/emsLog':require('./_component/send/emsLog'), //快递寄件-物流信息
    'send/status':require('./_component/send/sendStatus'), //快递寄件-日志信息 状态

    'login':require('./_component/uc/login'),               //登陆注册
    'uc/sms':require('./_component/uc/sms'),                   //短信订阅
    'uc/index':require('./_component/uc/index'),            //用户中心
    'uc/edit':require('./_component/uc/ucedit'),            //信息编辑
    'uc/edituser':require('./_component/uc/uiedit'),            //信息编辑
    'uc/edituserItem':require('./_component/uc/uiedit_item'),            //信息编辑
    'uc/faq':require('./_component/uc/faq'),                //常见问题
    'uc/address':require('./_component/uc/address'),        //地址
    'uc/addressedit':require('./_component/uc/addressedit'),        //地址
    'uc/addresslist':require('./_component/uc/addresslist'),//地址列表
    'uc/coupons':require('./_component/uc/coupons'),        //卡券列表
    'uc/coupondetail':require('./_component/uc/coupondetail'),//卡券详情
    'uc/order':require('./_component/uc/order'),            //订单
    'uc/message':require('./_component/uc/message'),        //消息中心
    'uc/messagedetail':require('./_component/uc/messagedetail'),//消息详情
    'uc/siteIntroduce':require('./_component/uc/siteIntroduce'),//网点介绍
    'uc/invitecode':require('./_component/uc/invitecode'),      //邀请码
    'uc/autolocation':require('./_component/uc/autolocation'),      //自动定位小区
    'uc/location':require('./_component/uc/location'),      //手动定位小区

    'express':require('./_component/express/express'),//我要收件
    'expLog':require('./_component/express/expLog'), //物流日志
    'evaluate':require('./_component/express/evaluate'),//评价
    'expFinish':require('./_component/express/expFinish'),//寄件完成

    'water/card':require('./_component/water/waterCard'),           //水卡列表
    'water/send':require('./_component/water/waterSend'),           //预约送水
    'water/item':require('./_component/water/waterItem'),           //预约送水-详情
    'water/sms':require('./_component/water/waterSms'),             //预约送水-短信订阅
    'water/comments':require('./_component/water/waterComments'),   //预约送水-评论
    'water/status':require('./_component/water/waterStatus')        //预约送水-日志信息
});


if (_login){
    api.req('/userInfo', {login: url.params.login, method: 'get'}, function(data){
        SA.append('USER', {info: data})
        distribute()
    })
}
else{
    router('login')
}

function distribute(){
    if(_hash){
        router(_hash)
    }else{
        if (url.hash){
            router(url.hash)
        }
        else{
            router('login')   //指定默认首页
        }
    }
}

libs.inject()
    .css(['/css/t/mobiscroll.min.css', 'mobiscroll_id'])
    .js(['/js/t/mobiscroll.2.17.0.min.js', 'Mobiscrolljs'])
